import 'package:azlistview/azlistview.dart';
import 'package:chat/controller/friend_list_controller.dart';
import 'package:chat/model/friend_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class FriendListScreen extends StatelessWidget {
  FriendListScreen({super.key});
  final FriendListController friendListController = Get.find();

  Widget customSearch() {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: TextField(
        //controller: searchController,
        decoration: InputDecoration(
          hintText: '搜索好友',
          hintStyle: TextStyle(color: Colors.grey[500]),
          prefixIcon: const Icon(Icons.search),
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(10),
            borderSide: BorderSide.none,
          ),
          filled: true,
          fillColor: Color(0XFFF2F3F7),
          contentPadding: const EdgeInsets.symmetric(vertical: 0),
        ),
        onChanged: (value) {
          //controller.searchGroups(value);
        },
      ),
    );
  }

  Widget customGroup() {
    return InkWell(
      onTap: () {
        Get.toNamed('/groupListScreen');
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16), // 调整内边距以接近 ListTile 默认样式
        child: Row(
          children: [
            // Leading 部分（头像/图标）
            Container(
              width: 36,
              height: 36,
              decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.circular(4.0),
              ),
              child: Image(image: AssetImage('assets/images/group.png')),
            ),
            SizedBox(width: 16), // 模拟 ListTile 的 leading 和 title 之间的间距
            // Title 部分
            Expanded(
              child: Text(
                '群聊',
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), // 可选：调整文字样式
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title: Text(
          '好友',
          style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.black),
        ),
        centerTitle: true,
      ),
      body: Obx(
        () => Column(
          children: [
            Expanded(
              child: AzListView(
                data: friendListController.friendList,
                itemCount: friendListController.friendList.length,
                itemBuilder: (BuildContext context, int index) {
                  if (index == 0) {
                    return customSearch();
                  } else if (index == 1) {
                    return customGroup();
                  } else {
                    Friend friend = friendListController.friendList[index];
                    return Container(
                      padding: EdgeInsets.symmetric(
                        vertical: 8,
                        horizontal: 16,
                      ), // 调整内边距以接近 ListTile 默认样式
                      child: Row(
                        children: [
                          // Leading 部分（头像/图标）
                          InkWell(
                            onTap: () {
                              Get.toNamed('/friendDetailScreen');
                            },
                            child: Container(
                              width: 36,
                              height: 36,
                              decoration: BoxDecoration(
                                shape: BoxShape.rectangle,
                                borderRadius: BorderRadius.circular(4.0),
                              ),
                              child: Image(
                                image: AssetImage('assets/images/zh-CN-XiaoqiuNeural.png'),
                              ),
                            ),
                          ),
                          SizedBox(width: 16), // 模拟 ListTile 的 leading 和 title 之间的间距
                          // Title 部分
                          Expanded(
                            child: InkWell(
                              onTap: () {
                                //friendChatController.backFlag = false;
                                Get.toNamed('/friendChatScreen');
                              },
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start, // 确保子项从顶部开始排列
                                children: [
                                  Text(
                                    friend.name,
                                    style: TextStyle(
                                      fontSize: 16,
                                      fontWeight: FontWeight.bold,
                                    ), // 可选：调整文字样式
                                  ),
                                  Text(
                                    friend.subTitle ?? "",
                                    style: TextStyle(fontSize: 10, color: Colors.grey), // 可选：调整文字样式
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    );
                  }
                },
                physics: BouncingScrollPhysics(),
                susItemBuilder: (BuildContext context, int index) {
                  Friend friend = friendListController.friendList[index];
                  if ('↑' == friend.getSuspensionTag()) {
                    return Container();
                  }
                  return Container(
                    height: 40,
                    width: MediaQuery.of(context).size.width,
                    padding: EdgeInsets.only(left: 16.0),
                    color: Color(0XFFF2F3F7),
                    alignment: Alignment.centerLeft,
                    child: Text(
                      friend.getSuspensionTag(),
                      softWrap: false,
                      style: TextStyle(fontSize: 14.0, color: Color(0xFF666666)),
                    ),
                  );
                },
                indexBarData: kIndexBarData,
                indexBarOptions: IndexBarOptions(
                  needRebuild: true,
                  ignoreDragCancel: true,
                  downTextStyle: TextStyle(fontSize: 12, color: Colors.white),
                  downItemDecoration: BoxDecoration(shape: BoxShape.circle, color: Colors.green),
                  indexHintWidth: 120 / 2,
                  indexHintHeight: 100 / 2,
                  indexHintDecoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/ic_index_bar_bubble_gray.png'),
                      fit: BoxFit.contain,
                    ),
                  ),
                  indexHintAlignment: Alignment.centerRight,
                  indexHintChildAlignment: Alignment(-0.25, 0.0),
                  indexHintOffset: Offset(-20, 0),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
